#include "bulk_storage.h"
#include <stdio.h>

int main() {
  struct memStore * ms;
  off_t block, block2;
  int x = 42, i;
  int *y;
  /* long blockCount; */
  printf("Hello world! -- Lets do some testing.");

  ms = ms_open((size_t)sizeof(int), (off_t)32);

  block2 = ms_newBlock(ms);

  block = ms_newBlock(ms);

  x = -1;
  ms_writeBlock(ms, block);

  y = (int*)ms_readBlock(ms, block);
  x = *y;
  printf("Block %ld contains %d Other block is %ld\n", (long int)block, x, (long int)block2);
  ms_releaseBlock(ms, block);

  /*  blockCount = ms.blockCount; */

  ms_close(ms);

  /*  ms = ms_open("TEST_file_store", "TEST_file_store.fat", sizeof(int), 0);


  y = (int*)ms_readBlock(ms, block);
  x = *y;
  printf("Block %ld contains %d Other block is %d\n", block, x, block2);
  ms_releaseBlock(ms, block, y);

  ms_close(ms);
  */
  printf("That was fun, now some performance evaluations.\nCreate 1,000,000 int[30] blocks sequentially:\n");

  ms = ms_open(sizeof(int[30]), 128);

  for(i = 0; i < 1000000; i++) {
    int blockIdx = ms_newBlock(ms);
    int *block = ms_readBlock(ms, blockIdx);
    int j;
    debug(TRIVIAL, ("ms_newBlock:\t%d\t", blockIdx));


    for(j = 0; j < 30; j++) {
      block[j] = i;
    }
    ms_writeBlock(ms, blockIdx);
    debug(TRIVIAL, (" %lx\n", (intptr_t)ms_readBlock(ms, blockIdx)));
    if(i && ! (i % 100000)) {
      debug(INFO, ("  %d00,000\n", i/100000));
      ms_freeBlock(ms, blockIdx);
    }
  }

  printf("1,000,000\n");


  ms_close(ms);

  return 0;
}
